package org.embedchat.protocol.message.server;

import org.embedchat.protocol.message.AbstractMessage;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
/**
 * Error message format:
 * <p/>
 * commandId - short
 * bodyLength - uint
 * messageCode - uint
 * errorMessageString - utf
 * 
 * 
 * @author koba
 *
 */
public class ErrorMessage extends AbstractMessage {

	private static final long serialVersionUID = 7229841560617390405L;
	
	/**
	 * Server return bad request string, if unparseble request is found
	 */
	public static final int BAD_REQUEST_ERROR = 1;
	
	/**
	 * Server return login error, if this command not accepteble on this moment
	 * As example, if client try to send TextMessage without login
	 */
	public static final int LOGIC_ERROR = 2;
	
	/**
	 * Internal server error 
	 */
	public static final int SERVER_ERROR = 3;
	
	private int messageCode;
	private String errorMessageString;
	
	public int getMessageCode() {
		return messageCode;
	}

	public void setMessageCode(int messageCode) {
		this.messageCode = messageCode;
	}

	public String getErrorMessageString() {
		return errorMessageString;
	}

	public void setErrorMessageString(String errorMessageString) {
		this.errorMessageString = errorMessageString;
	}

	@Override
	public String toString() {
		return "ErrorMessage: " + errorMessageString;
	}

	@Override
	public int getMessageBodyLength() {
		return 2 + errorMessageString.getBytes().length;
	}
	
	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof ErrorMessage))
			return false;
		ErrorMessage castOther = (ErrorMessage) other;
		return new EqualsBuilder().append(messageCode, castOther.messageCode)
				.append(errorMessageString, castOther.errorMessageString)
				.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(messageCode).append(
				errorMessageString).toHashCode();
	}
	
}
